package org.grottarossa.tms.ui.console.main;

import java.util.ResourceBundle;

import org.grottarossa.tms.ui.console.Console;
import org.grottarossa.tms.ui.console.ConsolleProviderService;
import org.grottarossa.tms.ui.console.LayoutProviderService;
import org.grottarossa.ui.core.ApplicationSession;
import org.grottarossa.ui.core.base.BasePanelView;
import org.grottarossa.ui.core.layout.LayoutProvider;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.vaadin.terminal.Sizeable;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.TabSheet;
import com.vaadin.ui.VerticalLayout;

/***
 * Rappresenta la struttura della console principale nella quale vengono caricate le sottoconsole
 * @author 
 *
 */
public class MainConsoleView extends BasePanelView<MainConsolePresenter>
implements ClickListener
{
	private TabSheet tsSections;
	private Button btnLogout;

	public MainConsoleView(ApplicationSession appSession, ResourceBundle localizer)
	{
		super(appSession, localizer);
		initializeComponents();
	}

	@Override
	protected void initializeComponents()
	{
		VerticalLayout appLayout = new VerticalLayout();
		appLayout.setWidth(100, Sizeable.UNITS_PERCENTAGE);
		appLayout.setHeight(100, Sizeable.UNITS_PERCENTAGE);

		btnLogout = new Button("Logout");
		btnLogout.addListener(this);
		HorizontalLayout header = new HorizontalLayout();
		header.setWidth(100, Sizeable.UNITS_PERCENTAGE);
		header.addComponent(btnLogout);
		header.setComponentAlignment(btnLogout, Alignment.MIDDLE_RIGHT);

        tsSections = new TabSheet();
        tsSections.setWidth(100, Sizeable.UNITS_PERCENTAGE);
		tsSections.setHeight(100, Sizeable.UNITS_PERCENTAGE);

		try
		{
			//Read spring context configuration files
			ApplicationContext contextConsoles = new ClassPathXmlApplicationContext("aeroflow-consoles.xml");
			ApplicationContext contextLayoutProvider = new ClassPathXmlApplicationContext("aeroflow-layoutproviders.xml");

			//add layout providers
			LayoutProviderService layoutService = (LayoutProviderService)contextLayoutProvider.getBean("TMSLayoutProviderService");

			for ( LayoutProvider layoutProvider : layoutService.getProviders())
			{
				layoutProvider.initialize(m_appSession);
				m_appSession.getLayoutFactory().add( layoutProvider );
			}

			//add consoles
			ConsolleProviderService consolleService = (ConsolleProviderService)contextConsoles.getBean("TMSConsolleProviderService");

			for ( Console console : consolleService.getConsoles())
			{
				console.initialize(m_appSession);
				console.getView().setWidth(100, Sizeable.UNITS_PERCENTAGE);
				console.getView().setHeight(100, Sizeable.UNITS_PERCENTAGE);
				VerticalLayout layout = new VerticalLayout ();
				layout.setWidth(100, Sizeable.UNITS_PERCENTAGE);
				layout.setHeight(100, Sizeable.UNITS_PERCENTAGE);
				layout.addComponent(console.getView());
				tsSections.addTab(layout, m_localizer.getString(console.getId()), null);
			}
		}
		catch (Exception e)
		{
			e.printStackTrace();
			showError("Something went wrong\n" + e.getMessage());
		}

		appLayout.addComponent(header);
		appLayout.addComponent(tsSections);
		appLayout.setExpandRatio(header, 1);
		appLayout.setExpandRatio(tsSections, 1000);
		setLayout(appLayout);
	}

	public void buttonClick(ClickEvent event)
	{
		m_presenter.logout();
	}
}
